﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace calculation.Models
{
    public class DetalRepository
    {
         private UchetDataContext _datacontext;

         public DetalRepository(UchetDataContext datacontext)
        {
            _datacontext = datacontext;
        }

         public IEnumerable<Detal> GetDetals()
         {
             return _datacontext.Detals.OrderBy(d => d.NameDetal);
         }

         public Detal GetDetals(int detalId)
         {
             return _datacontext.Detals.SingleOrDefault(d => d.DetalId == detalId);
         }
         public Detal CreateDetal(string nameDetal, string typeDetal)
         {
             Detal d = new Detal { NameDetal = nameDetal, TypeDetal = typeDetal };
             _datacontext.Detals.InsertOnSubmit(d);
             _datacontext.SubmitChanges();
             return d;
         }
         public void UpdateDetal(Detal d)
         {
             Detal dbDetal = GetDetals(d.DetalId);
             dbDetal.NameDetal = d.NameDetal;
             dbDetal.TypeDetal = d.TypeDetal;

             _datacontext.SubmitChanges();
         }

         public void DeleteDetal(int detalId)
         {
             Detal d = GetDetals(detalId);
             _datacontext.Detals.DeleteOnSubmit(d);
             _datacontext.SubmitChanges();
         }
    }
}